package com.pxmeta.app.controller.community;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pxmeta.circle.entity.CirclePrivilege;
import com.pxmeta.circle.service.ICirclePrivilegeService;
import com.pxmeta.common.constant.HttpStatus;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.enums.CircleUserLevelEnum;
import com.pxmeta.common.utils.AuthUtil;
import com.pxmeta.privilege.entity.Privilege;
import com.pxmeta.privilege.entity.dto.SetPrivilegeDTO;
import com.pxmeta.privilege.entity.vo.PrivilegeVO;
import com.pxmeta.privilege.service.IPrivilegeService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  权限API
 * </p>
 *
 * @author wcl
 * @since 2022-07-02
 */
@RestController
@RequestMapping("/privilege/privilege")
@RequiredArgsConstructor
@Api(tags = "圈子权限",value = "圈子权限")
public class PrivilegeController {

    private final IPrivilegeService privilegeService;
    private final ICirclePrivilegeService circlePrivilegeService;

    @SaCheckLogin
    @ApiOperation("获取权限列表")
    @GetMapping("/getList")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "circleId",value = "圈子id"),
                    @ApiImplicitParam(name = "userType", value = "1管理员，2普通成员"),
                    @ApiImplicitParam(name = "adminId", value = "管理员id")
            }
    )
    public AjaxResult<PrivilegeVO> getList(@RequestParam Long circleId,
                                           @RequestParam Integer userType,
                                           @RequestParam(required = false) Long adminId){
        PrivilegeVO vo = circlePrivilegeService.getCirclePrivilege(circleId, userType);
        return AjaxResult.success(vo);
    }





    @SaCheckLogin
    @ApiOperation("设置成员权限")
    @PutMapping("/setPrivilege")
    public AjaxResult setPrivilege(@RequestBody @Valid SetPrivilegeDTO dto){

        Long circleId = dto.getCircleId();
        String role = AuthUtil.getRole(circleId, CircleUserLevelEnum.CREATOR.getLevel());
        StpUtil.checkRole(role);

        Long privilegeId = dto.getPrivilegeId();
        Privilege privilege = privilegeService.getById(privilegeId);
        if (null == privilege){
            return AjaxResult.error(HttpStatus.PARAMS_ERROR, "权限不存在！");
        }
        CirclePrivilege cp = new CirclePrivilege();
        BeanUtils.copyProperties(dto, cp);
        LambdaQueryWrapper<CirclePrivilege> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CirclePrivilege::getCircleId, circleId)
                .eq(CirclePrivilege::getUserType, dto.getUserType())
                        .eq(CirclePrivilege::getPrivilegeId, dto.getPrivilegeId());
        CirclePrivilege circlePrivilege = circlePrivilegeService.getOne(wrapper);
        cp.setId(null == circlePrivilege ? null : circlePrivilege.getId());
        cp.setCode(privilege.getCode());
        circlePrivilegeService.saveOrUpdate(cp);
        return AjaxResult.success();
    }


}
